<?php

namespace app\admin\controller\sta;

use app\admin\model\Depot;
use app\admin\model\Disiname;
use app\admin\model\Disitype;
use app\admin\model\Factory;
use app\admin\model\m\Info;
use app\admin\model\Mafactory;
use app\common\controller\Backend;
use think\Db;
use think\exception\DbException;
use think\response\Json;

/**
 * 设备使用记录
 *
 * @icon fa fa-circle-o
 */
class Dlog extends Backend
{

    /**
     * Dlog模型对象
     * @var \app\admin\model\sta\Dlog
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\sta\Dlog;
        $this->view->assign("statusList", $this->model->getStatusList());
    }



    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //   全部批次为行,  条件是:   批次有出栏

//  TODO:  功能完成后, 需要改为: 查询完成上月的数据后,记录存到表 xxx, 并锁定数据不可修改
//  TODO:  查询条件:  月份,当月可修改;  (可能还会按厂区拆分)
        $infoModel = new \app\admin\model\m\Apply ;
        //  计算月份      *
        //   todo   需要改成可选月份
        //   查询本月数据是随动的,单条记录不可修改.  查询上月数据是已存到表 sta_material 的, 查询当月是即时的
        //   todo    $newdate  需要做成用户选择
        $newdate = date("Y-m-01");          // 月初       todo
        $newdate_31 = date("Y-m-d", strtotime("+1 month", strtotime($newdate) )  );    // 月底
        // 汇总月份
        $this->view->assign("mark", date("ym", strtotime($newdate) )."月" );

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        // 关联表查询
        $depotList = Depot::getList();
        $factoryList = Factory::getList();
        $mafactoryList = Mafactory::getList();
        $disinameList = Disiname::getList() ;
        $disitypeList = Disitype::getList() ;
        $infoList = Info::getList() ;

        //$mcategoryList = Mcategory::getList();

//print_r($where);die;
        $devices = Db::name("m_info")->where("mmaterial_id", 'eq', 9)->select();
        $device_id = array_keys( array_column($devices, null, 'id') );

        //  数据行:  设备领用
        $list = $infoModel
            ->where($where)
            ->where('info_id', 'in', $device_id)
            ->where('status', 'eq', '已通过'  )
            ->field("*")
            ->order("id ASC")
            ->paginate($limit);
//echo Db::getLastSql();die;
//        foreach($list as &$val){
//            $val['depot_id'] = $depotList[$val['depot_id']];
//            $val['mafactory_id'] = $mafactoryList[$val['mafactory_id']];

        // 计算月份
//            $val['createtime'] = $newdate;           //  todo
//        }

        // TODO:  如果计算合计,  那么要按当前筛选条件, 进行`不分页` 计算
//print_r($list);die;
        // TODO:  采集对应数据

        foreach($list as &$val){
            //$val['factory_id'] = $factoryList[$val['factory_id']] ;
            $val['admin_id'] = Db::name("admin")->where("id", $val['admin_id'])->find()['nickname'];
            $val['name'] = $infoList[$val['info_id']] ;
            $val['factory_id'] = Db::name('batch')->where("id", $val['batch_id'])->find()['factory_id'] ;     // 批次所在厂区   todo

            $val['a'] = $val['comment'] ;   // 用途   todo
            $val['b'] = $val['admin_id'] ;   //
            $val['c'] = $val['createtime'] ;  //
            $val['d'] = $val['returntime'] ;  //
        }
        $list = $this->getXXX($list);

        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }


}
